/*
 * CloudClient.h
 *
 *  Created on: Jan 23, 2012
 *      Author: hduser
 */

#ifndef CLOUDCLIENT_H_
#define CLOUDCLIENT_H_

#include<iostream>
#include<fstream>
#include<string>
#include "hdfs.h"
#include "Connection.h"
#include "Logger.h"
#include "ExceptionHandler.h"
#include "FileUtilities.h"

using namespace std;

namespace cloud {

struct chunkargs  {
	char*	edgeserver_host;
	int		edgeserver_port;
	int		VendorID;
	char*	hdfsfileName;
	int		chunkIndex;
	int		totalChunks;
};

const char* CMD_ACCEPT = "cmd_accept";
const char* ACK_ACCEPT = "ack_accept";


const char* CMD_DELETE="cmd_delete";
const char* ACK_DELETE="ack_delete";


const char* CMD_READY = "cmd__ready";
const char* CMD_ERROR = "cmd__error";
const char* CMD_SUCCESS = "cmdsuccess";
const char* CMD_NEWDATA = "cmd_nwdata";
const char* ACK_NEWDATA = "ack_nwdata";

const int CMD_LENGTH = 10;

class CloudClient {
public:
	CloudClient(Connection* conn);
	CloudClient(char* hostname, int port_number);
	virtual ~CloudClient();
	void init();
	void printMessage();
	Connection* cloudConn;
	Logger* logger;
	int writeFileToHDFS(char* localfileName, char* hdfsfileName);
	int getFileFromHDFS(char* hdfsfileName, char* localfilename);
	int calculateNumberOfChunks(char*  hdfsfileName);
	int scanCloud(char* sVendorID, char* cloudroot, char* mainserver_host, char* mainserver_port, char* cloudserver_host, char* cloudserver_port);
	int deleteFileFolderfromEdgeServer(int VendorID,char * fileName,int chunkIndex, char* edgeserver_host, int edgeserver_port);
	int getChunkFromHDFS(char*  hdfsfileName, int chunkIndex, int totalChunks, char** returnBuffer);
	size_t processChunk(int VendorID, char* hdfsfileName, int chunkIndex, int totalChunks, char* edgeserver_host, int edgeserver_port);
	hdfsFileInfo* getFileInfoFromHDFS(char* hdfsfileName);


private:
	static const int FILENAME_SIZE = 4096; // 4KB
	static const int BLOCK_SIZE = 4194304; // 4MB
	//static const size_t CHUNK_SIZE = 75 * BLOCK_SIZE;
	static const size_t CHUNK_SIZE = 1 * BLOCK_SIZE;
	static const int TCP_BUFFER_SIZE = 4096;
};

}

#endif /* CLOUDCLIENT_H_ */
